// Log.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "Log.h"
#include "config.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/* /////////////////////////////////////////////////////////////////////////
 * Set up fe.N and be.N.
 *
 * Requires:
 *
 * - PAN_BE_N_BACKEND_LIST - a null-entry terminated list of back-ends
 * - PAN_FE_N_PROCESS_IDENTITY - a character string defining the process
 *        identity
 * - PAN_FE_N_SEVERITY_CEILINGS - a 0-entry terminated list of severity
 *        ceilings for the backends identified in PAN_BE_N_BACKEND_LIST.
 *
 * NOTE: If these are defined in a C++ compilation unit, they must be
 *        marked 'extern "C++"'
 */

#ifdef __cplusplus
extern "C++"
{
#endif /* __cplusplus */

//pan_be_N_t  PAN_BE_N_BACKEND_LIST[] =
//{
//    PANTHEIOS_BE_N_ENTRY(0, 1, pantheios_be_WindowsDebugger)
//  , PANTHEIOS_BE_N_ENTRY(0, 2, pantheios_be_WindowsSyslog)
//  , PANTHEIOS_BE_N_ENTRY(0, 3, pantheios_be_WindowsEventLog)
//  , { 0, 0, NULL, NULL, NULL, NULL }
//};

//PANTHEIOS_EXTERN_C const char _PAN_FE_PROCESS_IDENTITY[]  =   "log.ossandcadTools";
/* Define the stock front-end process identity, so that it links when using
 * fe.N, fe.simple, etc. */
PANTHEIOS_EXTERN_C const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "log";

/* NOTE: fe.N processes these faster if the backEndId == index + 1 (as
 *   done below. Otherwise, it does a linear search.
 */
//const pan_fe_N_t  PAN_FE_N_SEVERITY_CEILINGS[]  =
//{
//    { 1,  PANTHEIOS_SEV_DEBUG   } /* Allows all severities */
//  , { 2,  PANTHEIOS_SEV_NOTICE  } /* Filters out everything below 'notice' */
//  , { 3,  PANTHEIOS_SEV_WARNING } /* Allows only 'warning', 'error', 'critical', 'alert', 'emergency' */
//  , { 0,  PANTHEIOS_SEV_NOTICE  } /* Terminates the array; sets the default ceiling to 'notice' */
//};

#ifdef __cplusplus
} /* extern "C++" */
#endif /* __cplusplus */

/* /////////////////////////////////////////////////////////////////////////
 * Set up be.WindowsEventLog
 *
 * Requires:
 *
 * - BE_WINDOWSEVENTLOG_EVENTID - an event log event id. We set this to 1000
 *        and assume that pantheios.COM is installed. See
 *        DllRegisterServer() for more details.
 * - pantheios_be_WindowsEventLog_calcCategory() - require to translate a
 *        severity to an event log category.
 *
 * NOTE: If these are defined in a C++ compilation unit, they must be
 *        marked 'extern "C++"'
 */

#ifdef __cplusplus
extern "C++"
{
#endif /* __cplusplus */

unsigned BE_WINDOWSEVENTLOG_EVENTID = 1002; /* This is the right one from pantheios.COM. Trust me! */

/* Forward declare, to placate CodeWarrior */
PANTHEIOS_CALL(unsigned) pantheios_be_WindowsEventLog_calcCategory(int backEndId, int severity);

PANTHEIOS_CALL(unsigned) pantheios_be_WindowsEventLog_calcCategory(int backEndId, int severity)
{
    STLSOFT_SUPPRESS_UNUSED(severity);
    STLSOFT_SUPPRESS_UNUSED(backEndId);

    return 0;
}

#ifdef __cplusplus
} /* extern "C++" */
#endif /* __cplusplus */

//
//TODO: If this DLL is dynamically linked against the MFC DLLs,
//		any functions exported from this DLL which call into
//		MFC must have the AFX_MANAGE_STATE macro added at the
//		very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//


// CLogApp

BEGIN_MESSAGE_MAP(CLogApp, CWinApp)
END_MESSAGE_MAP()


// CLogApp construction
CLogApp::CLogApp()
{
}


//! The one and only CLogApp object
CLogApp theApp;


// CLogApp initialization

BOOL CLogApp::InitInstance()
{
	using namespace pantheios;

	CWinApp::InitInstance();

	int panres =  pantheios::pantheios_init();

	if( panres < 0 )
    {
        OutputDebugStringA("Could not initialise the Pantheios logging libraries!\n");
		util::onBailOut(pantheios::emergency, "Failed to initialise the Pantheios libraries", PANTHEIOS_FE_PROCESS_IDENTITY, pantheios_getInitCodeString(panres));

        return FALSE;
    }
    else
    {
		pantheios_be_file_setFilePath(CConfig::getLogPath().c_str(), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_LOCAL);

		PANTHEIOS_TRACE_NOTICE("STARTING LOGGING");
    }

	return TRUE;
}

int CLogApp::ExitInstance()
{
	using namespace pantheios;

	PANTHEIOS_TRACE_NOTICE("STOPPING LOGGING");
	pantheios_uninit();
	return 0;
}